package com.tbit.uqbike.webmanager.business.photo_upload_log.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.RideLog;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.webmanager.business.photo_upload_log.dao.log.PhotoUploadLogDao;
import com.tbit.uqbike.webmanager.business.photo_upload_log.object.PhotoUploadLog;
import com.tbit.uqbike.webmanager.business.photo_upload_log.object.vo.PhotoReturnBikeStatVo;
import com.tbit.uqbike.webmanager.business.photo_upload_log.object.vo.PhotoUploadLogVo;
import com.tbit.uqbike.webmanager.business.photo_upload_log.service.PhotoUploadLogService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.RideLogDao;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 拍照/拍二维码还车日志
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2024/9/19
 */
@Slf4j
@RestController
@RequestMapping("/photoUploadLog")
@RequiredArgsConstructor
public class PhotoUploadLogController {
    final PhotoUploadLogService photoUploadLogService;

    /**
     * 获取拍照还车日志列表
     *
     * @param pageNO     页码
     * @param rowCount   每页数量
     * @param accountIds 区域id列表，逗号隔开
     * @param returnType 还车类型 1-拍照还车，2-拍二维码还车
     * @param result     还车结果，1-成功，2-失败
     * @param orderNO    订单号
     * @param userCode   车辆编号
     * @param phone      手机号
     * @param startTime  申诉开始时间
     * @param endTime    申诉结束时间
     */
    @GetMapping("/list")
    public Result<PageResult<PhotoUploadLogVo>> getByMachineId(@RequestParam(defaultValue = "1") Integer pageNO,
                                                               @RequestParam(defaultValue = "20") Integer rowCount,
                                                               @RequestParam @Pattern(regexp = "^\\d+(,\\d+)*$", message = "区域id列表格式错误") String accountIds,
                                                               Integer returnType,
                                                               Integer result,
                                                               String orderNO,
                                                               String phone,
                                                               String userCode,
                                                               Date startTime, Date endTime) {

        List<Integer> accountIdList = Arrays.stream(accountIds.split(","))
                .map(Integer::valueOf)
                .collect(Collectors.toList());

        return Result.success(photoUploadLogService.getPage(pageNO, rowCount, accountIdList, returnType, result, orderNO, userCode, startTime, endTime, phone));
    }

    /**
     * 导出拍照还车日志列表
     *
     * @param accountIds 区域id列表，逗号隔开
     * @param returnType 还车类型 1-拍照还车，2-拍二维码还车
     * @param result     还车结果，1-成功，2-失败
     * @param orderNO    订单号
     * @param userCode   车辆编号
     * @param phone      手机号
     * @param startTime  申诉开始时间
     * @param endTime    申诉结束时间
     */
    @GetMapping("/list/export")
    public JsonResult export(@RequestParam @Pattern(regexp = "^\\d+(,\\d+)*$", message = "区域id列表格式错误") String accountIds,
                             Integer returnType,
                             Integer result,
                             String orderNO,
                             String userCode,
                             String phone,
                             @RequestParam Date startTime, @RequestParam Date endTime) {

        List<Integer> accountIdList = Arrays.stream(accountIds.split(","))
                .map(Integer::valueOf)
                .collect(Collectors.toList());

        List<PhotoUploadLog> list = photoUploadLogService.getList(accountIdList, returnType, result, orderNO, userCode, startTime, endTime, phone);

        ExcelUtil<PhotoUploadLogVo> excelUtil = new ExcelUtil<>(PhotoUploadLogVo.class);
        return excelUtil.exportExcel(photoUploadLogService.transform(list, accountIdList), "拍照还车日志列表");
    }

    /**
     * 获取拍照还车日志统计列表
     *
     * @param pageNO     页码
     * @param rowCount   每页数量
     * @param accountIds 区域id列表，逗号隔开
     * @param startTime  申诉开始时间
     * @param endTime    申诉结束时间
     */
    @GetMapping("/stat")
    public Result<PageResult<PhotoReturnBikeStatVo>> getStat(@RequestParam(defaultValue = "1") Integer pageNO,
                                                        @RequestParam(defaultValue = "20") Integer rowCount,
                                                        @RequestParam @Pattern(regexp = "^\\d+(,\\d+)*$", message = "区域id列表格式错误") String accountIds,
                                                        @RequestParam Date startTime, @RequestParam Date endTime) {
        List<Integer> accountIdList = Arrays.stream(accountIds.split(","))
                .map(Integer::valueOf)
                .collect(Collectors.toList());

        PageInfo<PhotoReturnBikeStatVo> page = PageHelper.startPage(pageNO, rowCount)
                .doSelectPageInfo(() -> photoUploadLogService.getStat(accountIdList, startTime, endTime));

        return Result.success(PageResult.convertPage(page));
    }

    /**
     * 导出拍照还车日志统计列表
     *
     * @param accountIds 区域id列表，逗号隔开
     * @param startTime  申诉开始时间
     * @param endTime    申诉结束时间
     */
    @GetMapping("/stat/export")
    public JsonResult exportStat(@RequestParam @Pattern(regexp = "^\\d+(,\\d+)*$", message = "区域id列表格式错误") String accountIds,
                                 @RequestParam Date startTime, @RequestParam Date endTime) {

        List<Integer> accountIdList = Arrays.stream(accountIds.split(","))
                .map(Integer::valueOf)
                .collect(Collectors.toList());

        List<PhotoReturnBikeStatVo> list = photoUploadLogService.getStat(accountIdList, startTime, endTime);

        ExcelUtil<PhotoReturnBikeStatVo> excelUtil = new ExcelUtil<>(PhotoReturnBikeStatVo.class);
        return excelUtil.exportExcel(list, "拍照还车统计列表");
    }
}
